<?php
// @formatter:off
/**
 * @file c.tags.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-07-05 14:30:06 +0000 (Fri, 05 Jul 2013) $
 *
 * $Id: c.tags.php 89 2013-07-05 14:30:06Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0/themes/default/controlers/contents/c.tags.php $
 */
// @formatter:on

if($wcProfile->allowedTo(WC_PERM_TAG_LIST)) {
	require_once "{$this->_themeRoot}/controlers/generics/bundlepreview.php";
	require_once "{$this->_themeRoot}/controlers/generics/pager.php";

	global $wcThemeAssigns;
	global $wcAction;
	global $wcTagsHolder;
	global $wctDefaults;
	global $wcPageCache;
	global $wcGroup;

	$wcPageCache->anyTag();

	$page = isset($_REQUEST["page"]) ? $_REQUEST["page"] : 1;
	$minTagInPage = $wctDefaults["max-per-page"]["tags"] * ($page - 1);
	$maxTagInPage = $minTagInPage + $wctDefaults["max-per-page"]["tags"] - 1;

	$wcThemeAssigns["PATH"] = "All Tags (page {$page})";
	$wcThemeAssigns["ACTIONCONTENT"] = "/contents/c.tags.html";
	//
	// Selecting only tags in range.
	$tags = array();
	$tagCount = 0;
	foreach($wcTagsHolder->itemIds($wcGroup) as $id) {
		if($tagCount >= $minTagInPage && $tagCount < $maxTagInPage) {
			$tag = $wcTagsHolder->item($id, $wcGroup);
			if($tag->ok() && $tag->count) {
				$tags[] = $tag;
			}
		}
		$tagCount++;
	}

	wcSetPager(requestParams(true, $wcAction, array('page')), ceil($tagCount / $wctDefaults["max-per-page"]["tags"]), $page);

	//
	// Creating multi-array by prefix
	$lists = array();
	$ids = 1;
	foreach($tags as $tag) {
		$name = $tag->name;
		$prefix = strtoupper(isset($name[0]) ? $name[0] : "#");
		if($prefix < 'A' || $prefix > 'Z') {
			$prefix = '#';
		}
		$prefix = "{$prefix}";
		if(!isset($lists[$prefix])) {
			$lists[$prefix] = array(
				"prefix" => $prefix,
				"list" => array()
			);
		}

		$lists[$prefix]["list"][] = wcGetTagPreviewAssigns($tag);
	}
	ksort($lists);

	$wcThemeAssigns["TAGS-LISTS"] = $lists;
} else {
	$wcThemeAssigns["error-string"] = "You're not allowed to list tags";
	require "{$this->_themeRoot}/controlers/contents/c.403.php";
}
?>